<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use App\Kuafu\WaiMai\Channel\BaseWaiMai;
use App\Kuafu\WaiMai\Model\WaiMaiCallbackLogModel;
use App\Kuafu\WaiMai\Service\BaseWaiMaiOrderService;
use Closure;

class WaiMaiCallbackAfter extends Controller
{
    /**
     * 外卖回调的after middleware
     * @param $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (BaseWaiMaiOrderService::$callBackId) {
            $responseData = $response->getContent();
            if (!is_string($responseData)) {
                $responseData = json_encode($responseData);
            }

            WaiMaiCallbackLogModel::updateResponseData(
                BaseWaiMaiOrderService::$callBackId,
                BaseWaiMaiOrderService::$callBackShopId,
                $responseData,
                BaseWaiMaiOrderService::$errMsg
            );
        }

        return $response;
    }
}
